package com.wxl.common.base

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.lxj.xpopup.core.BasePopupView
import com.wxl.common.R
import com.wxl.common.util.KeyBoardUtil
import com.wxl.common.util.LayoutHelper
import com.wxl.common.wiget.SleepDialog
import kotlinx.android.synthetic.main.view_title_top_layout.*
import org.greenrobot.eventbus.EventBus


/**
 * create file time : 2021/3/1
 * create user : wxl
 * subscribe :
 */
abstract class AbsActivity : AppCompatActivity() {

    open var isCheckAppUpgrade = true

    open val popWindows = arrayListOf<BasePopupView>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setWindowStatusBar(true)


        setContentView(addTitleTopLayout())

        findViewById<TextView>(R.id.titleBackView).setOnClickListener {
            if (onBackClick()) {
                finish()
            }
        }

        findViewById<TextView>(R.id.titleTextView).text = getPageTitle()

        EventBus.getDefault().register(this)

        onCreateViewChanged()
    }


    open fun onBackClick(): Boolean {
        return true
    }


    open fun setMenu(menu: String, click: View.OnClickListener) {
        findViewById<TextView>(R.id.titleMenuView).text = menu
        findViewById<TextView>(R.id.titleMenuView).setOnClickListener(click)
    }

    open fun hideBackView() {
        findViewById<TextView>(R.id.titleBackView).visibility = View.GONE
    }


    open fun addTitleTopLayout(): View {
        return LayoutHelper.addTitleTopLayout(inflateLayout())
    }

    open fun inflateLayout(): ViewGroup {
        return View.inflate(this, getCreateContentViewId(), null) as ViewGroup
    }


    fun setTitleLayoutColor(color:Int){
        titleLayout.setBackgroundColor(color)
    }


    abstract fun getCreateContentViewId(): Int


    abstract fun onCreateViewChanged()


    abstract fun getPageTitle(): String


    internal open fun setWindowStatusBar(dark: Boolean) {
        val decor: View = window.getDecorView()
        if (dark) {
            decor.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            decor.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }

        window.statusBarColor = Color.TRANSPARENT
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        KeyBoardUtil.hideSoftInput(window.decorView)
        KeyBoardUtil.releaseObserver(this)
    }


    fun showPioUnEnablePop(context: Context) {
        SleepDialog(context).setOnClickListener(View.OnClickListener {
                finish()
        }).show()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}